package com.gid.rcflightlogger;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.orm.androrm.DatabaseAdapter;
import com.orm.androrm.Model;

//Base class for all other activities in this app.
//This base class takes care of common needs like creating global menu and database set up
public class BaseActivity extends Activity {
	static public class Colors
	{
		public static final int TABLE_ROW_BGHIGHLIGHT = 0xFFF77D00;
		public static final int TABLE_BORDER = 0x88909090;
		public static final int TEXT_INACTIVE_ITEM = 0xFF666666;
		
		public static final int TABLE_HEADER_UP = 0xFF94E090;
		public static final int TABLE_HEADER_DOWN = 0xFFEDA8A8;
		public static int TABLE_HEADER_NORMAL = 0;
	}
	
	public static final String ACK = "This is to acknowledge that this project uses Androrm, and excellent SQLite ORM layer for Android, provided at androrm.the-pixelpla.net";

	public static final String DB_NAME = "rcfl_db";
	
	protected int mCurrentMenuID = 0;
	protected String LOG_TAG = "BaseActivity";
	protected static int DIALOG_ID_USAGE = 200;
	protected static int DIALOG_ID_WHATSNEW = 201;
	
	//app preferences storage
	protected SharedPreferences mPrefs;
	protected Editor mPrefsEditor;
    
	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LOG_TAG = getClass().getSimpleName();
        setupDB();
        
        //set up prefrences reader/editor
		mPrefs = this.getPreferences(Activity.MODE_WORLD_READABLE);
		mPrefsEditor = mPrefs.edit();
		
		//get default color
		Colors.TABLE_HEADER_NORMAL = (new TextView(this)).getCurrentTextColor();
	}
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.menu_main, menu);
    	
    	MenuItem current_item = (MenuItem) menu.findItem(mCurrentMenuID);
    	if (current_item == null)
    	{
    		current_item = menu.findItem(R.id.menu_main_inventory).getSubMenu().findItem(mCurrentMenuID);
    	}
    	if (current_item == null)
    	{
    		current_item = menu.findItem(R.id.menu_main_logs).getSubMenu().findItem(mCurrentMenuID);
    	}
    	
    	if (current_item != null)
    	{
    		current_item.setEnabled(false);
    	}
    	return true;
	}
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == mCurrentMenuID)
        	return false;
        
        Intent i;
        switch (id)
        {
        case R.id.menu_main_flighttimer:
        	i = new Intent(this, FlightTimerActivity.class);
            startActivity(i);
            return true;
        case R.id.menu_main_inventory_aircrafts:
        	i = new Intent(this, AircraftBrowseActivity.class);
        	 startActivity(i);
             return true;
        case R.id.menu_main_inventory_batteries:
        	i = new Intent(this, BatteryBrowseActivity.class);
       	 	startActivity(i);
            return true;  
        case R.id.menu_main_logs_flights:
        	i = new Intent(this, FlightLogBrowseActivity.class);
       	 	startActivity(i);
            return true; 
        case R.id.menu_main_logs_cycles:
        	i = new Intent(this, BatteryLogBrowseActivity.class);
       	 	startActivity(i);
            return true;
        case R.id.menu_main_more_backuprestore:
        	i = new Intent(this, DatabaseBackupActivity.class);
       	 	startActivity(i);
            return true;
        case R.id.menu_main_more_usage:
        	showDialog(DIALOG_ID_USAGE);
        	return true;
        default:
        	return super.onOptionsItemSelected(item);
        }
    }
    
    @Override
    protected Dialog onCreateDialog(int id) {
    	
    	if (id == DIALOG_ID_USAGE)
    	{
    		AlertDialog.Builder builder = new AlertDialog.Builder(this);
    		builder.setIcon(R.drawable.myicon).setTitle("Usage instructions");
    		LinearLayout box = (LinearLayout)getLayoutInflater().inflate(R.layout.layout_usagedialog, null);
    		builder.setView(box);
    		builder.setNeutralButton("Close", new DialogInterface.OnClickListener() {
    			public void onClick(DialogInterface dialog, int id) {
    		}
    		});
    			
    		return builder.create();
    	}
    	else if (id == DIALOG_ID_WHATSNEW)
    	{
    		AlertDialog.Builder builder = new AlertDialog.Builder(this);
    		builder.setIcon(R.drawable.myicon).setTitle("What's new?");
    		ScrollView box = (ScrollView)getLayoutInflater().inflate(R.layout.layout_whatsnewdialog, null);
    		builder.setView(box);
    		builder.setNeutralButton("Close", new DialogInterface.OnClickListener() {
    			public void onClick(DialogInterface dialog, int id) {
    		}
    		});
    			
    		return builder.create();
    	}
    	else
    		return super.onCreateDialog(id);
    }
    
    
    /* (non-Javadoc)
	 * @see android.app.Activity#onNewIntent(android.content.Intent)
	 */
	@Override
	protected void onNewIntent(Intent intent) {
		// TODO Auto-generated method stub
		super.onNewIntent(intent);
	}

	private void setupDB() {
    	List<Class<? extends Model>> models = new ArrayList<Class<? extends Model>>();
    	models.add(Aircraft.class);
    	models.add(Flight.class);
    	models.add(Battery.class);
    	models.add(Cycle.class);
    	//models.add(Setting.class); //obsolete by the use of built in SharedPreferences class
    	
    	DatabaseAdapter.setDatabaseName(DB_NAME);
    	DatabaseAdapter adapter = new DatabaseAdapter(getApplicationContext());
    	adapter.setModels(models);
    }
}